package com.atguigu.ucenter.service.impl;

import com.atguigu.common.MD5;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.cron.vo.ResCollectVo;
import com.atguigu.common.cron.vo.ResMyCourseVo;
import com.atguigu.common.jwt.JwtUtils;
import com.atguigu.servicebase.exceptionhandler.ELeanExeception;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.LoginVo;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-02-03
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        //获取登录手机号 --- 此处存的邮箱账户
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new ELeanExeception(20001,"登录失败");
        }
        //判断手机号是否正确---邮箱账户
        Member m = selectByMobile(mobile);
        if (m == null){
            throw new ELeanExeception(20001,"登录失败");
        }
        if (!MD5.encrypt(password).equals(m.getPassword())){
            throw new ELeanExeception(20001,"登录失败");
        }
        if (m.getIsDisabled()){
            throw new ELeanExeception(20001,"登录失败");
        }
        return JwtUtils.getJwtToken(m.getId(),m.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)
        || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new ELeanExeception(20001,"注册失败");
        }

        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new ELeanExeception(20001,"注册失败");
        }

        int count = selectByCount(mobile);
        if (count > 0){
            throw new ELeanExeception(20001,"注册失败");
        }

        add(registerVo);

    }

    @Override
    public Member getMemberInfo(String memberId) {
        QueryWrapper<Member> qw=new QueryWrapper<>();
        qw.eq("id",memberId);
        qw.last("limit 1");
        return baseMapper.selectOne(qw);
    }

    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("openid",openid);
        qw.last("limit 1");
        return baseMapper.selectOne(qw);
    }

    /**
     * 获取当天注册总人数
     * @param day
     * @return
     */
    @Override
    public Integer countRegister(String day) {
        return baseMapper.selectCountRegister(day);
    }

    @Override
    public void editInfo(Member member) {
        baseMapper.updateInfoById(member);
    }

    @Override
    public PageListBean queryCoursesByMemberId(Integer pageNo, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNo,pageSize);
        List<ResMyCourseVo> myCourseVos= baseMapper.selectCoursesByMemberId(memberId);
        PageInfo pageInfo = new PageInfo(myCourseVos);
        return new PageListBean((int) pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加会员信息
     * @param registerVo
     * @return
     */
    private int add(RegisterVo registerVo){
        Member member = new Member();
        BeanUtils.copyProperties(registerVo,member);
        member.setPassword(MD5.encrypt(member.getPassword()));
        member.setIsDisabled(false); //用户不禁用
        member.setAvatar("https://gimg2.baidu.com/image_search");
        return baseMapper.insert(member);
    }

    /**
     * 根据手机号(邮箱账号)查询会员个数
     * @param mobile
     * @return
     */
    private int selectByCount(String mobile){
        QueryWrapper<Member> qw=new QueryWrapper<>();
        qw.eq("mobile",mobile);
        qw.last("limit 1");
        return baseMapper.selectCount(qw);
    }

    /**
     * 根据手机号(邮箱账号)查询会员
     * @param mobile
     * @return
     */
    private Member selectByMobile(String mobile){
        QueryWrapper<Member> qw=new QueryWrapper<>();
        qw.eq("mobile",mobile);
        qw.last("limit 1");
        return baseMapper.selectOne(qw);
    }
}
